package com.luminous.pick.sample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import com.bumptech.glide.Glide;
import com.cbs.application.activity.CBSActivity;
import com.luminous.pick.CustomGallery;
import com.luminous.pick.CustomGalleryActivity;
import com.luminous.pick.GalleryAdapter;

import java.util.ArrayList;

public class MainActivity extends CBSActivity {

    GridView gridGallery;
    Handler handler;
    GalleryAdapter adapter;

    ImageView imgSinglePick;
    Button btnGalleryPick;
    Button btnGalleryPickMul;

    ViewSwitcher viewSwitcher;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        init();
    }

    private void init() {
        handler = new Handler();
        gridGallery = (GridView) findViewById(R.id.imagepick_gridGallery);
        gridGallery.setFastScrollEnabled(true);
        adapter = new GalleryAdapter(getApplicationContext());
        gridGallery.setAdapter(adapter);

        viewSwitcher = (ViewSwitcher) findViewById(R.id.imagepick_viewSwitcher);
        viewSwitcher.setDisplayedChild(1);

        imgSinglePick = (ImageView) findViewById(R.id.imagepick_imgSinglePick);

        btnGalleryPick = (Button) findViewById(R.id.imagepick_btnGalleryPick);
        btnGalleryPick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CustomGalleryActivity.class);
                startActivityForResult(intent, 100);

            }
        });

        btnGalleryPickMul = (Button) findViewById(R.id.imagepick_btnGalleryPickMul);
        btnGalleryPickMul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CustomGalleryActivity.class);
                intent.putExtra(CustomGalleryActivity.Extra_SelectCount, 3);
                startActivityForResult(intent, 200);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
            adapter.clear();

            viewSwitcher.setDisplayedChild(1);
            String single_path = data.getStringExtra("single_path");
            Glide.with(this).load("file://" + single_path).into(imgSinglePick);
        } else if (requestCode == 200 && resultCode == Activity.RESULT_OK) {
            String[] all_path = data.getStringArrayExtra("all_path");

            ArrayList<CustomGallery> dataT = new ArrayList<>();

            for (String string : all_path) {
                CustomGallery item = new CustomGallery();
                item.sdcardPath = string;

                dataT.add(item);
            }

            viewSwitcher.setDisplayedChild(0);
            adapter.addAll(dataT);
        }
    }
}
